<?php

namespace app\admin\utils;

class Result
{
    public static function success($data = [])
    {
        return json([
            'code'    => 20000,
            'data'    => $data,
        ]);
    }

    /**
     * 错误
     * @param  mixed $code 错误码或错误信息
     */
    public static function error($code, $data = [])
    {
        $return = [
            'data' => $data
        ];
        // 如果是int，自动查找错误码，否则输出字符串
        if (is_int($code)) {
            $return['code'] = $code;
            $return['message'] = ErrorCode::message($code);
        } elseif (is_string($code)) {
            $return['code'] = 0;
            $return['message'] = $code;
        } elseif (is_array($code)) {
            $return['code'] = isset($code['code']) ? $code['code'] : 0;
            $return['message'] = isset($code['message']) ? $code['message'] : '';
        }
        return json($return);
    }

}